home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: philma@ix.netcom.com(Phil Majtan)
- Newsgroups: comp.lang.c
- Subject: Re: bubble sort walkthrough needed, please
- Date: 18 Apr 1996 00:27:12 GMT
- Organization: Netcom
- Message-ID: <4l4290$76g@cloner2.ix.netcom.com>
- References: <4l39b4$s2h@coranto.ucs.mun.ca>
- NNTP-Posting-Host: det-mi4-14.ix.netcom.com
- X-NETCOM-Date: Wed Apr 17 5:27:12 PM PDT 1996
-
- In <4l39b4$s2h@coranto.ucs.mun.ca> jdeeley@calvin.stemnet.nf.ca
- (J.Deeley) writes:
- >
- >Hi, everyone. I would appreciate your help on this problem that I am
- >sure you could code in your sleep.
- >
- >I am trying to understand how this bubble sort is working. I
- understand
- >the theory, and I understand how the whole thing works except for the
- >few lines under the comment /* now sort them using a bubble sort */
- >In other words, it's only the lines
- >
- >for(a=1; a<count; a++)
- > for(b=count-1; b>=a;--b)
- >
- >that I can't fathom. I've been working at this ever since last night
- and
- >I still don't get it. Could someone tell me what is going on in that
- >part?
- >
-
- It's easy to understand if you imagine a column of numbers and picture
- the pointers alongside. The a starts at the top and goes down to one
- less than the bottom. The b starts at the bottom and goes up to one
- less than the a. The b pointer compares to the a and if the value
- matches what we're looking for (greater) then the two numbers are
- swapped. In effect you are "bubbling" the numbers up to the top.
- Easy
- Phil
-
-